/**
 * 数据库信息配置相关JS
 */

$(function () {
    // 初始化表格
    var dg = $('#dg').datagrid({
        url: basePath + "/rptTools/rptMgr/queryDBCfgInfo",
        singleSelect: true,
        rownumbers:true,
        striped: true,
        remoteSort: true,
        pagination:true,//显示分页
        pageSize: 20,
        frozenColumns:[[
            {field:'sid',title:'数据库ID',width:80},
            {field:'dbName',title:'数据库名称',width:150},
            {field:'dbType',title:'数据库类型',width:80},
        ]],
        //pageList: [500,1000,2000],
        columns:[
            [
                {field:'jdbcURL',title:'数据库URL',width:350},
                {field:'userName',title:'数据库用户名',width:150},
                {field:'password',title:'数据库密码',width:150},
                {field:'dbCommment',title:'描述',width:300},
                {field:'operate',title:'操作',width:300,align:'center',formatter:function(value,row,index){
                    var $row = JSON.stringify(row).replace(/\"/g,"'");
                    return '<a style="color:#4697e5; text-decoration:none;padding-right:15px;" onClick="dbCfg.editDBCfg(' +  $row + ')" href="#">修改配置</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:15px;" onClick="dbCfg.delDBCfg(\'' +  row.dbName + '\',' + row.sid + ')" href="#">删除配置</a>';
                }}
            ]
        ]
    });
});

var dbCfg = {
    /**
     * 查询数据
     */
    query : function() {
        var dbName = $('#dbName').val();  // 数据库名称

        var param = {};
        param.dbName = dbName;
        $('#dg').datagrid({
            url:basePath + "/rptTools/rptMgr/queryDBCfgInfo",
            queryParams:param
        });
    },
    addDBcfg : function () {
        this.resetWin();
        $('#dbcfgWin').window('open');

    },
    editDBCfg : function (dbCfg) {
        this.resetEditWin();
        var dbId = dbCfg.sid;
        var dbName = dbCfg.dbName;
        var dbURL = dbCfg.jdbcURL;
        var dbUser = dbCfg.userName;
        var dbPwsd = dbCfg.password;
        var dbType = dbCfg.dbType;
        var dbCommment = dbCfg.dbCommment;
        $("#dbId").val(dbId);
        $("#editDBName").textbox('setValue',dbName);
        $("#editDBURL").textbox('setValue',dbURL);
        $("#editDBDesc").textbox('setValue',dbCommment);
        $("#editDBUser").textbox('setValue',dbUser);
        $("#editDBPwsd").textbox('setValue',dbPwsd);
        $("#editDBType").textbox('setValue',dbType);
        $('#editDBCfgWin').window('open');
    },
    delDBCfg : function (dbName,dbId) {
        var confirmMsg = '确认删除' + dbName + '吗?'
        var func = this;
        $.messager.confirm('提示', confirmMsg, function(r){
            if (r){
                func.showMask();
                $.ajax({
                    url:basePath + "/rptTools/rptMgr/deleteDBCfg",
                    type:'POST',
                    dataType:'json',
                    data:{
                        dbId: dbId,
                        dbName: dbName
                    },
                    async: false,
                    success:function(data){
                        $("#mask").hide();
                        var status = data.status;
                        var errorMsg = data.errorMsg;
                        if(status == "1") {
                            alert("删除数据库配置成功");
                            $('#dg').datagrid({
                                url:basePath + "/rptTools/rptMgr/queryDBCfgInfo"
                            });
                        } else {
                            alert(errorMsg);
                        }
                    }
                })
            }
        });
    },
    showMask : function () {
        $("#mask").css("height",$(document).height());
        $("#mask").css("width",$(document).width());
        $("#mask").text("正在删除...");
        $("#mask").show();
    },
    saveDBCfg : function () {
        var errorMsg = '';
        var errorFlag = false;
        var dbName = $("#instDBName").val();
        var dbType = $("#instDBType").val();
        var dbURL = $("#instDBURL").val();
        var dbDesc = $("#instDBDesc").val();
        var dbUser = $("#instDBUser").val();
        var dbPwsd = $("#instDBPwsd").val();
        if(dbName == '') {
            errorMsg = '数据库名称不能为空';
            errorFlag = true;
        }else if(dbType == '') {
            errorMsg = '数据库类型不能为空';
            errorFlag = true;
        }else if(dbURL == '') {
            errorMsg = '数据库URL不能为空';
            errorFlag = true;
        }else if(dbUser == '') {
            errorMsg = '数据库用户不能为空';
            errorFlag = true;
        }else if(dbPwsd == '') {
            errorMsg = '数据库密码不能为空';
            errorFlag = true;
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.dbName = dbName;
        param.dbType = dbType;
        param.dbURL = dbURL;
        param.dbDesc = dbDesc;
        param.dbUser = dbUser;
        param.dbPwsd = dbPwsd;
        this.showMask();
        $.ajax({
            url:basePath + "/rptTools/rptMgr/addDBCfg",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                $("#mask").hide();
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("新增数据库配置成功");
                    $('#dbcfgWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/rptMgr/queryDBCfgInfo"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    updateDBCfg : function () {
        var errorMsg = '';
        var errorFlag = false;
        var dbName = $("#editDBName").val();
        var dbType = $("#editDBType").val();
        var dbURL = $("#editDBURL").val();
        var dbDesc = $("#editDBDesc").val();
        var dbUser = $("#editDBUser").val();
        var dbPwsd = $("#editDBPwsd").val();
        var dbId = $("#dbId").val();
        if(dbName == '') {
            errorMsg = '数据库名称不能为空';
            errorFlag = true;
        }else if(dbType == '') {
            errorMsg = '数据库类型不能为空';
            errorFlag = true;
        }else if(dbURL == '') {
            errorMsg = '数据库URL不能为空';
            errorFlag = true;
        }else if(dbUser == '') {
            errorMsg = '数据库用户不能为空';
            errorFlag = true;
        }else if(dbPwsd == '') {
            errorMsg = '数据库密码不能为空';
            errorFlag = true;
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.dbName = dbName;
        param.dbType = dbType;
        param.dbURL = dbURL;
        param.dbDesc = dbDesc;
        param.dbUser = dbUser;
        param.dbPwsd = dbPwsd;
        param.dbId = dbId;
        this.showMask();
        $.ajax({
            url:basePath + "/rptTools/rptMgr/updateDBCfg",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                $("#mask").hide();
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("修改数据库配置成功");
                    $('#editDBCfgWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/rptMgr/queryDBCfgInfo"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    resetWin : function () {
        $("#instDBName").textbox('setValue','');
        $("#instDBURL").textbox('setValue','');
        $("#instDBDesc").textbox('setValue','');
        $("#instDBUser").textbox('setValue','');
        $("#instDBPwsd").textbox('setValue','');
    },
    resetEditWin : function () {
        $("#editDBName").textbox('setValue','');
        $("#editDBURL").textbox('setValue','');
        $("#editDBDesc").textbox('setValue','');
        $("#editDBUser").textbox('setValue','');
        $("#editDBPwsd").textbox('setValue','');
    },
    closeWin : function (winId) {
        $('#' + winId).window('close');
    }
}
